package com.woniu.zhoukao.consumer;

import com.rabbitmq.client.Channel;
import com.woniu.zhoukao.entity.Order;
import com.woniu.zhoukao.mapper.OrderMapper;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;

@Component
public class DelayCancelOrderConsumer {

    @Autowired
    OrderMapper orderMapper;

    @RabbitListener(queues = "deadLetterQueue")
    public void onMsg(Integer orderId, Channel channel, Message message) throws IOException {

        System.out.println("-----------------DelayCancelOrderConsumer 收到消息 "+orderId);

        //根据订单id查询订单信息,如果订单还是未支付状态(1),那就将状态改为已取消(3)
        Order order = orderMapper.selectById(orderId);
        if(order.getStatus() == 1){
            order.setStatus(3);
            orderMapper.updateById(order);
        }


        //手动ack
        channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);

    }




}
